package com.hj.boot.controller;

import com.hj.boot.pojo.BaseDict;
import com.hj.boot.pojo.Customer;
import com.hj.boot.pojo.QueryVo;
import com.hj.boot.service.BaseDictService;
import com.hj.boot.service.CustomerService;
import com.hj.boot.util.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.expression.spel.ast.TypeCode;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by hj on 2019/5/13.
 * 客户管理
 */
@Controller
public class CustomerController {

    @Autowired
    private BaseDictService baseDictService;
    @Autowired
    private CustomerService customerService;
    /**
     * @Value注入外部参数
     */
    @Value("${customer.source.code}")
    private String custSorceCode;
    @Value("${customer.industry.code}")
    private String custIndustryCode;
    @Value("${customer.level.code}")
    private String custLevelCode;

    @RequestMapping("/customer/list")
    public String showCustomerList(QueryVo queryVo, Model model) throws Exception {
        //乱码处理，没有乱码就不需要加了
//        if (StringUtils.isNotBlank(queryVo.getCustName())) {
//            queryVo.setCustName(new String(queryVo.getCustName().getBytes("iso8859-1"), "utf-8"));
//        }
        //初始化，客户来源
        List<BaseDict> sourceList = baseDictService.getDictListByTypeCode(custSorceCode);
        //所属行业
        List<BaseDict> industryList = baseDictService.getDictListByTypeCode(custIndustryCode);
        //客户级别
        List<BaseDict> levelList = baseDictService.getDictListByTypeCode(custLevelCode);
//        通过model去把字段信息传递到页面
        model.addAttribute("fromType",sourceList);
        model.addAttribute("industryType",industryList);
        model.addAttribute("levelType",levelList);
        /**
         * 根据查询条件查询客户列表
         */
        Page<Customer> page = customerService.getCustomerList(queryVo);
//      把客户列表传递到页面
        model.addAttribute("page",page);
//      参数回显
        model.addAttribute("custName",queryVo.getCustName());
        model.addAttribute("custSource",queryVo.getCustSource());
        model.addAttribute("custIndustry",queryVo.getCustIndustory());
        model.addAttribute("custLevel",queryVo.getCustLevel());
        return "customer";
    }

    @RequestMapping("/customer/edit")
    @ResponseBody
    public Customer getCustomerById(Long id){
        Customer customer = customerService.getCustomerById(id);
        return customer;
    }

    @RequestMapping(value="/customer/update",method = RequestMethod.POST)
    @ResponseBody
    public String updateCustomer(Customer customer){
        customerService.updateCustomer(customer);
        return "有钱一直爽，一直有钱一直爽";
    }

    @RequestMapping("/customer/delete")
    @ResponseBody
    public String deleteCustomer(Long id){
        customerService.deleteCustomer(id);
        return "ok";
    }

}
